package com.changgou.goods.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.service.BrandService;
import com.changgou.pojo.Brand;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author gentleman Ma
 * @Date created in 2020/2/11
 * @description  品牌控制器
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/brand")
public class BrandController {


    @Autowired
    private BrandService brandService;

    /**
     * 查询所有品牌
     * @return
     */
    @GetMapping("findAll")
    public Result findAll(){
     List<Brand> brandList= brandService.findAll();
     return new Result(true, StatusCode.OK,"查询所有品牌成功",brandList);
    }


    /**
     * 根据id查询品牌
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Integer id){
      Brand brand =  brandService.findById(id);
        return new Result(true,StatusCode.OK,"根据品牌ID查询成功",brand);
    }


    /**
     * 新增品牌数据
     * @param brand
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true,StatusCode.OK,"添加品牌成功");
    }


    /**
     * 根据id修改品牌信息
     * @param brand
     * @param id
     * @return
     */
    @PutMapping("/update/{id}")
    public Result update(@RequestBody Brand brand ,@PathVariable("id") Integer id){
        brand.setId(id);
        brandService.update(brand);
        return new Result(true,StatusCode.OK,"修改品牌成功");

    }

    /**
     * 根据id删除品牌
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
        brandService.delete(id);
        return new Result(true,StatusCode.OK,"根据id删除品牌成功");
    }

    /**
     * 多条件搜索商品查询
     * @param searchMap
     * @return
     */
    @GetMapping("/search")
    public Result findList(@RequestParam Map<String, Object> searchMap){
        List<Brand> list= brandService.findList(searchMap);
        return new Result(true,StatusCode.OK,"多条件搜索成功",list);
    }


    /**
     * 分页搜索实现
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/findPage/{page}/{size}")
    public Result findPage(@PathVariable("page") int page,@PathVariable("size") int size){
        Page<Brand> pageList = brandService.findPage(page,size);
        PageResult pageResult = new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"分页搜索成功",pageResult);
    }


    /**
     * 分页加搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/findSearchPage/{page}/{size}")
    public Result findSearchPage(@RequestParam Map<String, Object> searchMap,
                                 @PathVariable("page") int page, @PathVariable("size") int size){
      //  int i = 1/0;
        Page<Brand> pageList2= brandService.findSearchPage(searchMap,page,size);
        PageResult pageResult = new PageResult(pageList2.getTotal(),pageList2.getResult());
        return new Result(true,StatusCode.OK,"分页加条件搜索成功",pageResult);
    }


    /**
     * 根据分类名称查询品牌列表
     * @param categoryName
     * @return
     */
    @GetMapping("/category/{categoryName}")
    public Result findListByCategoryName(@PathVariable("categoryName") String categoryName){
        log.info("[分类名称]categoryName:{}",categoryName);
        List<Brand> brandList = brandService.findListByCategoryName(categoryName);
        return new Result(true,StatusCode.OK,"根据分类名称查询品牌列表成功",brandList);
    }


}
